<?php
namespace jy_liangoods;
defined('BY_JYA') or exit('error');
/*
	订单，V8打印机：58mm（32字符），V9：80mm（42字符）
	设置字体大小、居中及切纸、来单提示音：http://www.jinyunzn.com/manage/index.php?plugin=core&action=article.show&nosession=1&id=11（出处：进云手册）
*/
class printmsg extends \plugin{
	//抢单打印机播报
	public function newgetpint($sendlog=array()){
		$params=array('uniacid'=>$_SESSION['uniacid'],'print_status'=>1);
		$params['merchid']=$order['merchid'];
		$params['print_type']=2;
		//$printers=pdo_getall('jy_weishop_printer',$params);
		foreach($printers as $p){
			if($p['type']==7 || $p['type']==9){
				$this->plugin('jy_feieyun')->printer($printparams,'jy_liangoods',true,$p['print_nums'],$p['tpl_type']);
			}elseif($p['type']==8 || $p['type']==10){
				model('jy_xpyun/model')->sendprint($printparams['id'],$printparams['sendcontent'],true,$p['print_nums'],$p['tpl_type']);
			}else{
				$this->plugin('jy_print')->printer($printparams,'jy_liangoods',true,$p['print_nums']);
			}
		}
		return false;
	}
	public function neworder($sendlog=array()){
		$ordergoods=json_decode($sendlog['goods'],true);
		$this->orderprint('',$sendlog['order_id'],1,0,0,'jy_liangoods',$ordergoods,true,2);
		return false;
	}
	public function orderprint($merchid='',$order_id='',$is_pay=0,$daike=0,$printerid=0,$pluginname='',$ordergoods=array(),$isadd=false,$print_type=0){
		$isprint=false;
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$order['merchid']=$merchid;
		$order['api_data']=json_decode($order['api_data'],true);
		if(!$ordergoods){
			return false;
		}
		$conttags=model('jy_weishop/printtpl')->upconttags($order,$is_pay,$daike,$printerid,$pluginname,$ordergoods,$isadd);
		$params=array('uniacid'=>$_SESSION['uniacid'],'print_status'=>1);
		if($order['merchid']){
			$params['merchid']=$order['merchid'];
		}
		if($print_type){
			$params['print_type']=$print_type;
		}else{
			$params['print_type <=']=3;
		}
		if($printerid){
			$printers=pdo_getall('jy_weishop_printer',array('id'=>$printerid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$printers=pdo_getall('jy_weishop_printer',$params);
		}
		foreach($printers as $p){
			$newordergoods=$ordergoods;
			if($p['goodstype']){
				if($p['goodstype']==1 && $p['goodspids']){
					//按商品分类打印
					$goods=array();
					foreach($ordergoods as $ov){
						if(strpos($p['goodspids'],';'.$ov['pcate'].';') !== false){
							$goods[]=$ov;
						}
					}
					$p['ordergoods']=$goods;
				}elseif($p['goodstype']==2){
					//指定商品打印
					$p['goods']=json_decode($p['goods'],true);
					$setgoods=';';
					foreach($p['goods'] as &$p){
						$setgoods .=$p['id'].';';
					}
					$goods=array();
					foreach($ordergoods as $ov){
						if(strpos($setgoods,';'.$ov['goodsid'].';') !== false){
							$goods[]=$ov;
						}
					}
					$p['ordergoods']=$goods;
				}
				$newordergoods=$p['ordergoods'];
			}
			if(count($newordergoods)<1){
				continue;
			}
			//单商品打印,单商品标签打印
			if($p['tpl_type']==1 || $p['tpl_type']==2 || $p['tpl_type']==3){
				$pgoods=array();
				foreach($newordergoods as $ov){
					$pgoods[0]=$ov;
					$goodsstr=$this->getgoodsstr($pgoods,$p['tpl_type'],$p['type']);
					$textinfo=array(
						'goodsinfo'=>$goodsstr['goodsinfo'],
						'goodsinfo2'=>$goodsstr['goodsinfo2'],
						'goodsinfoold'=>$goodsstr['goodsinfo'],
						'conttags'=>$conttags,
					);
					$sendcontent=model('jy_weishop/printtpl')->upcontent($order,$textinfo,$p,$pluginname,$newordergoods,$isadd);
					model('jy_weishop/printmsg')->toprint($p,$sendcontent,$order,$daike,$print_type);
				}
			}else{
				if($p['goodstype']){
					$conttags=model('jy_weishop/printtpl')->upconttags($order,$is_pay,$daike,$p['type'],$pluginname,$newordergoods,$isadd);
				}
				$goodsstr=$this->getgoodsstr($newordergoods,'',$p['type']);
				$textinfo=array(
					'goodsinfo'=>$goodsstr['goodsinfo'],
					'goodsinfo2'=>$goodsstr['goodsinfo2'],
					'goodsinfoold'=>$goodsstr['goodsinfo'],
					'conttags'=>$conttags,
				);
				$sendcontent=model('jy_weishop/printtpl')->upcontent($order,$textinfo,$p,$pluginname,$newordergoods,$isadd);
				model('jy_weishop/printmsg')->toprint($p,$sendcontent,$order,$daike,$print_type);
			}
			pdo_update('jy_weishop_order',array('isprint'=>1),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			$isprint=true;
		}
		return $isprint;
	}
	public function getgoodsstr($ordergoods,$tpl_type=0,$ptype=2){
			$str=$strnew = '';
			foreach($ordergoods as $g){
				if(!$g['title']){
					$info=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']),array('title'));
					$g['title']=$info['title'];
				}
				if($tpl_type==2 || $tpl_type==3){
					return array('goodsinfo'=>$g);
				}
				if($ptype==1 || $ptype==8 || $ptype==9){
					$title0=cfc('tools')->substr($g['title'],0,10);
					$title0w=20;
					$moneyw=6;
					$conttags['line']=$this->strlen_pad('-',30,'-')."\n";
				}elseif($ptype==2 || $ptype==3 || $ptype==7  || $ptype==10){
					$title0=cfc('tools')->substr($g['title'],0,15);
					$title0w=32;
					$moneyw=11;
					$conttags['line']=$this->strlen_pad('-',47,'-')."\n";
				}
				$money = $g['price'] * $g['total'];
				$money=number_format($money, 2);
				if($g['optionid']){
					$strnew .= $this->strlen_pad($title0,$title0w)."\n";
					$strnew .= $this->strlen_pad('-['.$g['optiondata'].']',$title0w);
				}else{
					$strnew .= $this->strlen_pad($title0,$title0w);
				}
				if(!$tpl_type){
					$strnew .= $this->strlen_pad(' ',$moneyw);
					$strnew .= $this->strlen_pad($g['total'],4). "\n";
				}else{
					$strnew .= $this->strlen_pad(' ',$moneyw);
					$strnew .= $this->strlen_pad($g['total'],6). "\n";
				}
			}
			if($tpl_type){
				$goodsinfoold='';
				$goodsinfo='';
			}else{
				$strtop=$this->strlen_pad('品名',$title0w);
				$strtop .= $this->strlen_pad(' ',$moneyw);
				$strtop .= $this->strlen_pad('数量',4);
				$goodsinfo="<B>".$strtop."\n</B>";
			}
			$goodsinfo .= $strnew;
			$goodsinfo2 .= "<S2>".$strnew."</S2>";
			$goodsinfo .= $conttags['line'];
			$goodsinfo2 .= $conttags['line'];
			return array('goodsinfo2'=>$goodsinfo2,'goodsinfo'=>$goodsinfo);
	}
	public function sendNotice($params){
		$tag = ((isset($params['tag']) ? $params['tag'] : ''));
		$touser = ((isset($params['openid']) ? $params['openid'] : ''));
		if (empty($touser)) {
			return;
		}
	}
	public function strlen_pad($str,$alllen,$zifu='')//补齐空格
	{
		//真实字符串长度（一个中文=2）
		$len = (strlen($str)+mb_strlen($str,'UTF-8'))/2;
		$buqi = '';
		for($i=0;$i<($alllen-$len);$i++){
			$buqi .= $zifu?:' ';
		}
		$new_str = $str.$buqi;
		return $new_str;
	}
}
